package com.etennis.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etennis.constants.AppHttpCodeEnum;
import com.etennis.exception.SystemException;
import com.etennis.mapper.EventsMapper;
import com.etennis.mapper.MemberMapper;
import com.etennis.mapper.RecordMapper;
import com.etennis.pojos.dto.app.RecordDTO;
import com.etennis.pojos.entity.Events;
import com.etennis.pojos.entity.Member;
import com.etennis.pojos.entity.Record;
import com.etennis.pojos.vo.app.RecordVO;
import com.etennis.service.IEventsService;
import com.etennis.service.IMemberService;
import com.etennis.service.IRecordService;
import com.etennis.utils.BeanCopyUtils;
import com.etennis.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    EventsMapper eventsMapper;
    @Autowired
    IEventsService eventsService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IRecordService recordService;
    /**
     * 插入Record表
     * @param recordDTO
     */
    @Override
    public void saveRecord(RecordDTO recordDTO) {
        Record record = BeanCopyUtils.copyBean(recordDTO, Record.class);
        Events events = eventsMapper.selectOne(new LambdaQueryWrapper<Events>().eq(Events::getName, recordDTO.getEventName()));
        if(events==null)
            throw new SystemException(AppHttpCodeEnum.RECORD_INPUT_ERROR);
        record.setEventId(events.getId());
        Member a1 = inputDealer(new LambdaQueryWrapper<Member>().eq(Member::getName, recordDTO.getWinA1Name()));
        record.setWinA1Id(a1.getId());

        if(recordDTO.getWinA2Name()!=null&&recordDTO.getWinA2Name().length()!=0){
            Member a2=inputDealer(new LambdaQueryWrapper<Member>().eq(Member::getName, recordDTO.getWinA2Name()));
        record.setWinA2Id(a2.getId());}

        Member b1 = inputDealer(new LambdaQueryWrapper<Member>().eq(Member::getName, recordDTO.getLoseB1Name()));
        record.setLoseB1Id(b1.getId());

        if(recordDTO.getLoseB2Name()!=null&&recordDTO.getLoseB2Name().length()!=0){
        Member b2 = inputDealer(new LambdaQueryWrapper<Member>().eq(Member::getName, recordDTO.getLoseB2Name()));
        record.setLoseB2Id(b2.getId());}

        record.setFlag(recordDTO.getFlag());

        System.out.println(SecurityUtils.getLoginUserId());
        if(record.getWinA1Id().equals(SecurityUtils.getLoginUserId())||(record.getWinA2Id()!=null&&record.getWinA2Id().equals(SecurityUtils.getLoginUserId())))
        record.setIsFinished("10");
        else record.setIsFinished("01");
        recordMapper.insert(record);
    }

    Member inputDealer(LambdaQueryWrapper queryWrapper){
        Member member = memberMapper.selectOne(queryWrapper);
        if(Objects.isNull(member))
            throw new SystemException(AppHttpCodeEnum.RECORD_INPUT_ERROR);
        return member;
    }
    @Override
    public List<RecordVO> unfinishedRecord() {
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Record::getWinA2Id, SecurityUtils.getLoginUserId()).or()
                .eq(Record::getLoseB2Id,SecurityUtils.getLoginUserId()).or()
                .eq(Record::getLoseB1Id,SecurityUtils.getLoginUserId()).or()
                .eq(Record::getWinA1Id,SecurityUtils.getLoginUserId());
        List<Record> list = recordService.list(queryWrapper);
        List<RecordVO> recordVOS = BeanCopyUtils.copyBeanList(list, RecordVO.class);
        for (RecordVO recordVO : recordVOS) {
            recordVO.setEventName(eventsService.getById(recordVO.getEventId()).getName());
            recordVO.setWinA1Name(memberService.getById(recordVO.getWinA1Id()).getName());
            recordVO.setLoseB1Name(memberService.getById(recordVO.getLoseB1Id()).getName());
            if(recordVO.getFlag().equals("2")){
                recordVO.setWinA2Name(memberService.getById(recordVO.getWinA2Id()).getName());
                recordVO.setLoseB2Name(memberService.getById(recordVO.getLoseB2Id()).getName());

            }
            recordVO.setScore(16);
            recordVO.setIsWin(recordVO.getWinA1Id().equals(SecurityUtils.getLoginUserId())||(recordVO.getWinA2Id()!=null&&recordVO.getWinA2Id().equals(SecurityUtils.getLoginUserId()))?"1":"0");
        }
        return recordVOS;
    }
    @Override
    public void updateRecord(RecordDTO recordDTO) {
        Record record = BeanCopyUtils.copyBean(recordDTO, Record.class);
        Events events = eventsMapper.selectOne(new LambdaQueryWrapper<Events>().eq(Events::getName, recordDTO.getEventName()));
        record.setEventId(events.getId());
        Member a1 = memberMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getName, recordDTO.getWinA1Name()));
        record.setWinA1Id(a1.getId());
        Member a2 = memberMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getName, recordDTO.getWinA2Name()));
        record.setWinA2Id(a2.getId());
        Member b1 = memberMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getName, recordDTO.getLoseB1Name()));
        record.setLoseB1Id(b1.getId());
        Member b2 = memberMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getName, recordDTO.getLoseB2Name()));
        record.setLoseB2Id(b2.getId());
        recordMapper.updateById(record);
    }

}
